package com.jingyanzi.finance.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.finance.dao.CashCashierMapper;
import com.jingyanzi.finance.dao.CashPlaceMapper;
import com.jingyanzi.finance.dao.CashStatisticsMapper;
import com.jingyanzi.finance.domain.CashCashierDO;
import com.jingyanzi.finance.domain.CashStatisticsDO;
import com.jingyanzi.finance.entity.CashCashier;
import com.jingyanzi.finance.entity.CashPlace;
import com.jingyanzi.finance.entity.CashStatistics;
import com.jingyanzi.finance.entity.CashStatisticsExample;
import com.jingyanzi.finance.entity.CashStatisticsExample.Criteria;
import com.jingyanzi.finance.query.CashCashierQueryDO;
import com.jingyanzi.finance.query.CashStatisticsQueryDO;
import com.jingyanzi.finance.result.ResultDO;
import com.jingyanzi.finance.result.ResultSupport;
import com.jingyanzi.finance.service.ICashierService;
import com.jingyanzi.finance.service.IPayRecordService;
import com.jingyanzi.finance.service.IStatisticsService;
import com.jingyanzi.finance.util.BeanUtilsExtends;

@Service("statisticsService")
public class StatisticsServiceImpl extends BaseServiceImpl implements IStatisticsService {

	@Autowired
	private CashStatisticsMapper cashStatisticsMapper;

	@Autowired
	private ICashierService cashierService;

	@Autowired
	private IPayRecordService payRecordService;

	@Autowired
	private CashCashierMapper cashCashierMapper;

	@Autowired
	private CashPlaceMapper cashPlaceMapper;

	@Override
	public void task() throws Exception {

		CashCashierQueryDO query = new CashCashierQueryDO();
		query.setPage(false);
		ResultDO res = cashierService.query(query);
		List<CashCashierDO> list = (List<CashCashierDO>) res.getModel(ResultDO.FIRST_MODEL_KEY);

		for (CashCashierDO cashCashier : list) {
			CashStatistics entity = new CashStatistics();
			entity.setCreatetime(Calendar.getInstance().getTime());
			ResultDO res1 = payRecordService.dayStatistics(cashCashier);
			CashStatisticsDO domain = (CashStatisticsDO) res1.getModel(ResultSupport.FIRST_MODEL_KEY);
			domain.setCashierName(cashCashier.getName());
			domain.setCashierId(cashCashier.getId());
			domain.setPlaceId(cashCashier.getPlaceId());
			domain.setPlaceName(cashCashier.getPlaceName());
			ResultSupport result = BeanUtilsExtends.copy(entity, domain);
			entity.setCreatetime(Calendar.getInstance().getTime());
			// 0未对账 1相符 2不相符
			entity.setStatus(0);
			if (!result.isSuccess())
				throw new Exception("对象复制错误");

			try {
				cashStatisticsMapper.insert(entity);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public ResultDO modifiStatus(CashStatisticsDO cashStatisticsDO) {

		CashStatistics entity = new CashStatistics();
		ResultSupport result = new ResultSupport();

		int r = -1;
		entity.setId(cashStatisticsDO.getId());
		entity.setStatus(cashStatisticsDO.getStatus());
		try {
			r = cashStatisticsMapper.updateByPrimaryKeySelective(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (r < 1) {
			result.setSuccess(false);
		}

		return result;
	}

	@Override
	public ResultDO get(int id) {

		CashStatistics entity = null;

		ResultSupport result = new ResultSupport();

		try {
			entity = cashStatisticsMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (entity == null) {
			result.setSuccess(false);
			return result;
		}

		CashCashierDO cashCashierDO = new CashCashierDO();
		cashCashierDO.setId(entity.getCashierId());
		cashCashierDO.setPlaceId(entity.getPlaceId());
		ResultDO res1 = payRecordService.dayStatistics(cashCashierDO);
		CashStatisticsDO cashStatisticsDO = getCashStatisticsDO(entity);
		CashStatisticsDO domain = (CashStatisticsDO) res1.getModel(ResultSupport.FIRST_MODEL_KEY);

		cashStatisticsDO.setCashTypeDO(domain.getCashTypeDO());
		cashStatisticsDO.setPayMethodDO(domain.getPayMethodDO());
		cashStatisticsDO.setRealAmount(cashStatisticsDO.getAmount().subtract(cashStatisticsDO.getAccountAmount()));
		cashStatisticsDO.setCreatetime(entity.getCreatetime().getTime() + "");
		result.setModel(ResultDO.FIRST_MODEL_KEY, cashStatisticsDO);

		return result;
	}

	@Override
	public ResultDO queryStatement(CashStatisticsQueryDO query) {

		ResultSupport result = new ResultSupport();
		if (query.getStartTimeStamp() == null || query.getEndTimeStamp() == null) {

			result.setErrorCode(ResultSupport.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "时间为空");
			result.setSuccess(false);
		}

		CashCashierDO cashCashierDO = new CashCashierDO();
		cashCashierDO.setId(query.getCashierId());
		cashCashierDO.setPlaceId(query.getPlaceId());

		cashCashierDO.setStartTimeStamp(query.getStartTimeStamp());
		cashCashierDO.setEndTimeStamp(query.getEndTimeStamp());
		// 查询金额
		ResultDO res1 = payRecordService.dayStatistics1(cashCashierDO);

		CashStatisticsDO domain = (CashStatisticsDO) res1.getModel(ResultSupport.FIRST_MODEL_KEY);

		
		// 查询收银员名称
		if (query.getCashierId() != null && query.getCashierId() != 0) {

			CashCashier selectByPrimaryKey = cashCashierMapper.selectByPrimaryKey(query.getCashierId());

			domain.setCashierName(selectByPrimaryKey.getName());
		}
		// 查询收银点
		if (query.getPlaceId() != null && query.getPlaceId() != 0) {
			CashPlace selectByPrimaryKey = cashPlaceMapper.selectByPrimaryKey(query.getPlaceId());
			domain.setPlaceName(selectByPrimaryKey.getPlaceName());
		}

		domain.setEndTimeStamp(query.getEndTimeStamp());
		domain.setStartTimeStamp(query.getStartTimeStamp());

		result.setModel(ResultDO.FIRST_MODEL_KEY, domain);

		return result;
	}

	@Override
	public ResultDO query(CashStatisticsQueryDO query) {

		ResultSupport result = new ResultSupport();

		List<CashStatistics> list = null;

		CashStatisticsExample example = new CashStatisticsExample();

		Criteria createCriteria = example.createCriteria();

		if (StringUtils.isNotEmpty(query.getStartTimeStamp()) && StringUtils.isNotEmpty(query.getEndTimeStamp())) {
			Date startTime = new Date(Long.parseLong((query.getStartTimeStamp() + (24 * 60 * 60 * 1000))));
			Date endTime = new Date(Long.parseLong((query.getEndTimeStamp() + (24 * 60 * 60 * 1000))));
			createCriteria.andCreatetimeBetween(startTime, endTime);
		}

		if (query.getPlaceId() != 0) {
			createCriteria.andPlaceIdEqualTo(query.getPlaceId());
		}

		if (query.isPage()) {
			example.setLimitByPage(query.getStart() + "," + query.getPageRows());
			long count = cashStatisticsMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		List<CashStatisticsDO> doList = new ArrayList<CashStatisticsDO>();

		try {
			list = cashStatisticsMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}
		BigDecimal totalAmount = new BigDecimal(0.00);
		for (CashStatistics cashStatistics : list) {
			totalAmount = totalAmount.add(cashStatistics.getAmount());
		}
		doList = getCashStatisticsDOList(list);

		result.setModel(ResultSupport.FIRST_MODEL_KEY, doList);
		result.setModel(ResultSupport.THIRD_MODEL_KEY, totalAmount);
		return result;
	}

	public CashStatisticsDO getCashStatisticsDO(CashStatistics entity) {
		if (entity == null)
			return null;
		CashStatisticsDO dst = new CashStatisticsDO();
		return BeanUtilsExtends.copyProperties(dst, entity) ? dst : null;
	}

	public List<CashStatisticsDO> getCashStatisticsDOList(List<CashStatistics> list) {
		List<CashStatisticsDO> result = new ArrayList<CashStatisticsDO>();
		if (list != null && !list.isEmpty()) {
			for (CashStatistics org : list) {
				CashStatisticsDO org1 = getCashStatisticsDO(org);
				if (org1 != null) {
					org1.setCreatetime(org.getCreatetime().getTime() + "");
					org1.setRealAmount(org1.getAmount().subtract(org1.getAccountAmount()));
					result.add(org1);
				}
			}
		}
		return result;
	}

}
